﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Menus
{
    /// <summary>
    /// 表示菜单特性
    /// </summary>
    public class MenuAttribute : Attribute
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// Action
        /// </summary>
        public string ActionName { get; set; }
        /// <summary>
        ///  Controller
        /// </summary>
        public string ControllerName { get; set; }
        /// <summary>
        /// 路由数据
        /// </summary>
        public object RouteValues { get; set; }

        /// <summary>
        /// 表示菜单特性
        /// </summary>
        private MenuAttribute()
        {
            this.ActionName = "index";
        }

        /// <summary>
        /// 表示菜单特性
        /// </summary>
        /// <param name="name">菜单名</param>
        /// <param name="controller">控制器名</param>
        public MenuAttribute(string name, string controller)
            : this()
        {
            this.Name = name;
            this.ControllerName = controller;
        }

        /// <summary>
        /// 表示菜单特性
        /// </summary>
        /// <param name="name">菜单名</param>
        /// <param name="controller">控制器名</param>
        /// <param name="action">Action名</param>
        public MenuAttribute(string name, string controller, string action)
            : this(name, controller)
        {
            this.ActionName = action;
        }

        /// <summary>
        /// 转换到菜单
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public Menu ToMenu(UrlHelper helper)
        {
            var link = helper.Action(this.ActionName, this.ControllerName, this.RouteValues);
            return new Menu { Name = this.Name, Link = link.ToLower() };
        }
    }
}